package com.powerbank.common.config;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Sentinel配置类
 */
@Configuration
public class SentinelConfig {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    @PostConstruct
    public void initFlowRules() {
        // 配置限流规则
        List<FlowRule> flowRules = new ArrayList<>();
        
        // 用户中心限流规则
        FlowRule userCenterRule = new FlowRule();
        userCenterRule.setResource("user-center");
        userCenterRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        userCenterRule.setCount(100); // QPS阈值为100
        flowRules.add(userCenterRule);
        
        // 支付中心限流规则
        FlowRule paymentCenterRule = new FlowRule();
        paymentCenterRule.setResource("payment-center");
        paymentCenterRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        paymentCenterRule.setCount(200); // QPS阈值为200
        flowRules.add(paymentCenterRule);
        
        // 订单中心限流规则
        FlowRule orderCenterRule = new FlowRule();
        orderCenterRule.setResource("order-center");
        orderCenterRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        orderCenterRule.setCount(150); // QPS阈值为150
        flowRules.add(orderCenterRule);
        
        // 设备中心限流规则
        FlowRule deviceCenterRule = new FlowRule();
        deviceCenterRule.setResource("device-center");
        deviceCenterRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        deviceCenterRule.setCount(120); // QPS阈值为120
        flowRules.add(deviceCenterRule);
        
        // 财务中心限流规则
        FlowRule financeCenterRule = new FlowRule();
        financeCenterRule.setResource("finance-center");
        financeCenterRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        financeCenterRule.setCount(100); // QPS阈值为100
        flowRules.add(financeCenterRule);
        
        // 运营后台限流规则
        FlowRule adminSystemRule = new FlowRule();
        adminSystemRule.setResource("admin-system");
        adminSystemRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        adminSystemRule.setCount(50); // QPS阈值为50
        flowRules.add(adminSystemRule);
        
        FlowRuleManager.loadRules(flowRules);
    }

    @PostConstruct
    public void initDegradeRules() {
        // 配置降级规则
        List<DegradeRule> degradeRules = new ArrayList<>();
        
        // 用户中心降级规则
        DegradeRule userCenterDegradeRule = new DegradeRule();
        userCenterDegradeRule.setResource("user-center");
        userCenterDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        userCenterDegradeRule.setCount(0.5); // 异常比例阈值为50%
        userCenterDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(userCenterDegradeRule);
        
        // 支付中心降级规则
        DegradeRule paymentCenterDegradeRule = new DegradeRule();
        paymentCenterDegradeRule.setResource("payment-center");
        paymentCenterDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        paymentCenterDegradeRule.setCount(0.3); // 异常比例阈值为30%
        paymentCenterDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(paymentCenterDegradeRule);
        
        // 订单中心降级规则
        DegradeRule orderCenterDegradeRule = new DegradeRule();
        orderCenterDegradeRule.setResource("order-center");
        orderCenterDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        orderCenterDegradeRule.setCount(0.4); // 异常比例阈值为40%
        orderCenterDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(orderCenterDegradeRule);
        
        // 设备中心降级规则
        DegradeRule deviceCenterDegradeRule = new DegradeRule();
        deviceCenterDegradeRule.setResource("device-center");
        deviceCenterDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        deviceCenterDegradeRule.setCount(0.3); // 异常比例阈值为30%
        deviceCenterDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(deviceCenterDegradeRule);
        
        // 财务中心降级规则
        DegradeRule financeCenterDegradeRule = new DegradeRule();
        financeCenterDegradeRule.setResource("finance-center");
        financeCenterDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        financeCenterDegradeRule.setCount(0.2); // 异常比例阈值为20%
        financeCenterDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(financeCenterDegradeRule);
        
        // 运营后台降级规则
        DegradeRule adminSystemDegradeRule = new DegradeRule();
        adminSystemDegradeRule.setResource("admin-system");
        adminSystemDegradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        adminSystemDegradeRule.setCount(0.2); // 异常比例阈值为20%
        adminSystemDegradeRule.setTimeWindow(10); // 降级时间窗口为10秒
        degradeRules.add(adminSystemDegradeRule);
        
        DegradeRuleManager.loadRules(degradeRules);
    }
}